package com.xiaobias.distributed.pan.web;

import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping({"/"})
public class HomeController {

    @Resource
    private ClientRegistrationRepository clientRegistrationRepository;

    @GetMapping("/login")
    public String getLoginPage(Model model) {
        Map<String, String> oauth2AuthenticationUrls = new HashMap<>();
        Iterable<ClientRegistration> clientRegistrations;
        clientRegistrations = (InMemoryClientRegistrationRepository) clientRegistrationRepository;
        clientRegistrations.forEach(registration -> oauth2AuthenticationUrls
                .put(registration.getClientName(), "oauth2/authorization/" + registration.getRegistrationId())
        );
        model.addAttribute("urls", oauth2AuthenticationUrls);
        return "/user/login";
    }
}